package cn.lingyangwl.agile.member.model.entity;

import cn.lingyangwl.agile.model.BasePlusPO;
import cn.lingyangwl.agile.tenant.core.DataIsolation;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.handlers.AbstractJsonTypeHandler;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

import java.math.BigDecimal;

/**
 * 会员等级
 *
 * @author shenguangyang
 */
@Getter
@Setter
@TableName(value = "member_level", autoResultMap = true)
@DataIsolation(filterMainTenant = true, tableName = "member_level")
public class MemberLevel extends BasePlusPO {
	/**
	 * 会员等级1
	 */
	public static final String LEVE_1 = "1";

    @TableId
	private Long id;
	
	/**
	 * 会员名称
	 */
	private String name;
	
	/**
	 * 等级权重, 数字越大 等级越高
	 */
	private String weight;
	
	/**
	 * 背景图片
	 */
	private String backgroundUrl;
	
	/**
	 * 升级条件, 满足条件后自动升级, eg: expendMoney = 100
	 */
	@TableField(typeHandler = UpgradeHandler.class)
	private Upgrade upgrade;
	
	/**
	 * 是否为默认等级：0-&gt;不是；1-&gt;是
	 */
	private Boolean isDefault;
	
	/**
	 * 免运费标准
	 */
	private BigDecimal freeFreightPoint;
	
	/**
	 * 每次评价获取的成长值
	 */
	private Integer commentGrowthPoint;
	
	/**
	 * 消费折扣权益,  0: 不打折, 9.8代表98折
	 */
	private BigDecimal equityConsumeDiscount;
	
	/**
	 * 积分回馈倍率权益, 回馈积分 = 消费金额 * 积分回馈倍率
	 */
	private BigDecimal equityPointFeedback;
	
	/**
	 * 是否有免邮权益
	 */
	private Boolean equityFreeFreight;
	
	/**
	 * 是否有签到权益
	 */
	private Boolean equitySignIn;
	
	/**
	 * 是否有评论获奖励权益
	 */
	private Boolean equityComment;
	
	/**
	 * 是否有专享活动权益
	 */
	private Boolean equityPromotion;
	
	/**
	 * 是否有会员价格权益
	 */
	private Boolean equityMemberPrice;
	
	/**
	 * 是否有生日权益
	 */
	private Boolean equityBirthday;
	
	/**
	 * 赠送积分
	 */
	private Integer sendPoint;
	
	/**
	 * 赠送红包
	 */
	private BigDecimal sendBalance;
	
	/**
	 * 状态，
	 * @see cn.lingyangwl.agile.model.enums.BoolEnum
	 */
	private String status;
	
	/**
	 * 会员数量
	 */
	private Long memberNum;

    public static LambdaQueryWrapper<MemberLevel> lqw() {
		return Wrappers.lambdaQuery();
	}

	public static LambdaUpdateWrapper<MemberLevel> luw() {
		return Wrappers.lambdaUpdate();
	}

	/**
	 * 升级条件, 多个条件之间是与的关系
	 */
	@Data
	public static class Upgrade {
		/**
		 * 期望金额
		 */
		private BigDecimal expendMoney;

		/**
		 * 所需成长值
		 */
		private Integer experience;
	}

	public static class UpgradeHandler extends AbstractJsonTypeHandler<Upgrade> {
		@Override
		protected Upgrade parse(String json) {
			return JSON.parseObject(json, Upgrade.class);
		}

		@Override
		protected String toJson(Upgrade obj) {
			return JSON.toJSONString(obj).trim();
		}
	}
}
